﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using mozhi.smarterp.BasicData.Customers;
using mozhi.smarterp.BasicData.Products.Repositories;
using mozhi.smarterp.BasicData.RemoteRepositories;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Volo.Abp.Modularity;
using Mozhi.Abp.Domain.AuthorCenter;
using Mozhi.Abp.Domain.AuthorCenter.Repository;
using mozhi.smarterp.BasicData.Warehouses;
using Mozhi.Abp.OperationLog;
using Mozhi.Abp.Module.DataLinkage.EntityFrameworkCore;

namespace mozhi.smarterp.BasicData.EntityFrameworkCore;

[DependsOn(
    typeof(BasicDataDomainModule),
    typeof(AbpEntityFrameworkCoreModule),
    typeof(DataLinkageEntityFrameworkCoreModule)
)]
public class BasicDataEntityFrameworkCoreModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAbpDbContext<BasicDataDbContext>(options =>
        {
                /* Add custom repositories here. Example:
                 * options.AddRepository<Question, EfCoreQuestionRepository>();
                 */
        });

        Configure<AbpEntityOptions>(options =>
        {
            options.Entity<Customer>(op =>
            {
                op.DefaultWithDetailsFunc = query => query.Include(o => o.ContactItems);
            });

            options.Entity<Supplier>(op =>
            {
                op.DefaultWithDetailsFunc = query => query.Include(o => o.ContactItems);
            });

            options.Entity<Warehouse>(op =>
            {
                op.DefaultWithDetailsFunc = query => query.Include(o => o.Locations);
            });
        });

        context.Services.AddScoped(typeof(IDepartmentRepository), typeof(DepartmentRepository));
        context.Services.AddScoped(typeof(IProductInventoryRepository), typeof(ProductInventoryRepository));
        context.Services.AddScoped(typeof(IProductInventorySettingRepository), typeof(ProductInventorySettingRepository));
        context.Services.AddScoped(typeof(IProductPurchaseSettingRepository), typeof(ProductPurchaseSettingRepository));
        context.Services.AddScoped(typeof(IOperationLogger), typeof(DefaultOperationLogger));
    }
}
